package com.yrc.controller;

import com.yrc.pojo.Category;
import com.yrc.pojo.UserAddress;
import com.yrc.pojo.bo.AddressBO;
import com.yrc.pojo.bo.ShopcartBO;
import com.yrc.service.AddressService;
import com.yrc.utils.MobileEmailUtils;
import com.yrc.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: Yrc
 * @create: 2020/11/18 22:02
 */
@Api(value = "地址相关",tags = "地址相关的api接口")
@RestController
@RequestMapping("address")
public class AddressController {


    /**
     * 用户在确认订单页面，可以针对收货地址做如下操作
     * 1.查询用户的所有收货地址
     * 2.新增收货地址
     * 3.删除收货地址
     * 4.修改收货地址
     * 5.设置默认地址
     */

    @Autowired
    private AddressService addressService;

    @ApiOperation(value = "根据用户id查询收货地址列表",notes = "根据用户id查询收货地址列表",httpMethod = "POST")
    @PostMapping("/list")
    public R list(
            @RequestParam String userId) {
        if(StringUtils.isBlank(userId)){
            return R.errorMsg("");
        }
        List<UserAddress> list = addressService.queryAll(userId);
        return R.ok(list);
    }

    @ApiOperation(value = "用户新增地址",notes = "用户新增地址",httpMethod = "POST")
    @PostMapping("/add")
    public R add(@RequestBody AddressBO addressBO) {

        R checkRes = checkAddress(addressBO);
        if (checkRes.getStatus() != 200) {
            return checkRes;
        }

        addressService.addNewUserAddresss(addressBO);

        return R.ok();
    }

    private R checkAddress(AddressBO addressBO){
        String receive = addressBO.getReceiver();
        if (StringUtils.isBlank(receive)) {
            return R.errorMsg("收货人不能为空");
        }
        if (receive.length() > 12) {
            return R.errorMsg("收货人姓名不能太长");
        }

        String mobile = addressBO.getMobile();
        if (StringUtils.isBlank(mobile)){
            return R.errorMsg("收货人手机号不能为空");
        }
        if (mobile.length() != 11) {
            return R.errorMsg("收货人手机号长度不正确");
        }
        boolean isMobileOk = MobileEmailUtils.checkMobileIsOk(mobile);
        if (!isMobileOk) {
            return R.errorMsg("收货人手机号格式不正确");
        }

        String province = addressBO.getProvince();
        String city = addressBO.getCity();
        String district = addressBO.getDistrict();
        String detail = addressBO.getDetail();
        if (StringUtils.isBlank(province) ||
        StringUtils.isBlank(city) ||
        StringUtils.isBlank(district) ||
        StringUtils.isBlank(detail)) {
            return R.errorMsg("收货地址信息不能为空");
        }

        return R.ok();
    }

    @ApiOperation(value = "用户修改地址",notes = "用户修改地址",httpMethod = "POST")
    @PostMapping("/update")
    public R update(@RequestBody AddressBO addressBO) {

        if(StringUtils.isBlank(addressBO.getAddressId())){
            return R.errorMsg("修改地址错误:addressId不能为空");
        }

        R checkRes = checkAddress(addressBO);
        if (checkRes.getStatus() != 200) {
            return checkRes;
        }

        addressService.updateUserAddress(addressBO);

        return R.ok();
    }

    @ApiOperation(value = "用户删除地址",notes = "用户删除地址",httpMethod = "POST")
    @PostMapping("/delete")
    public R delete(@RequestParam String userId,
                    @RequestParam String addressId) {

        if(StringUtils.isBlank(userId) || StringUtils.isBlank(addressId)) {
            return R.errorMsg("");
        }

        addressService.deleteUserAddress(userId,addressId);
        return R.ok();
    }

    @ApiOperation(value = "用户设置默认地址",notes = "用户设置默认地址",httpMethod = "POST")
    @PostMapping("/setDefalut")
    public R setDefault(@RequestParam String userId,
                    @RequestParam String addressId) {

        if(StringUtils.isBlank(userId) || StringUtils.isBlank(addressId)) {
            return R.errorMsg("");
        }

        addressService.updateUserAddressToBeDefault(userId,addressId);
        return R.ok();
    }

}
